/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components;

import java.util.HashMap;
import java.util.Map;
import org.balisunrise.application.Application;
import org.balisunrise.common.Basic;
import org.balisunrise.common.Messenger;
import org.balisunrise.common.PropertiesReader;
import org.balisunrise.common.SelectionPanelField;
import org.balisunrise.common.defaults.VEAUtil;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

/**
 * Classe que controla as mensagens de campos de seleção.
 * @author glauco
 */
public class SelectionMessenger {

    /**
     * Instância das Selection Menssenger.
     */
    private static final Map<String, SelectionMessenger> instances =
            new HashMap<String, SelectionMessenger>();

    /**
     * Identificador para exibir a mensagem
     */
    private String id;

    /**
     * Messenger que exibe as mensagens
     */
    private Messenger messenger;

    /**
     * Construtor privado
     * @param id identificador
     * @param basic algum Basic.
     */
    private SelectionMessenger(String id, Basic basic){
        this.id = id;
        messenger = VEAUtil.getMessenger(basic);
    }

    /**
     * Adquire uma instância a partir de um SelectionPanelField
     * @param owner algum SelectionPanelField
     * @return a instância para o SelectionPanelField
     */
    public static SelectionMessenger getInstance(SelectionPanelField owner){
        if(owner == null)
            return null;

        String id = owner.getName();

        if(instances.containsKey(id))
            return instances.get(id);

        SelectionMessenger sm = new SelectionMessenger(id,owner);
        instances.put(id, sm);

        return sm;
    }

    public  void putMessage(String message){

        if(message == null || message.isEmpty())
            message = Application.get()
                    .getPropertiesReader()
                    .readProperty(PropertiesReader.messageINVALID_INPUT);

        Messenger m = getMessenger();
        if(m != null){
            m.addMessage(id, message, 3);
        }else{
            NotifyDescriptor d = new NotifyDescriptor
                    .Message(message,NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(d);
        }
    }

    public void clearMessage(){
        Messenger m = getMessenger();
        if(m != null){
            m.removeMessage(id);
        }
    }

    /**
     * retorna o messenger.
     */
    private Messenger getMessenger() {
        return messenger;
    }
}
